﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace DataConsistency.UC
{
    public partial class DuLieuThuNghiem : UserControl
    {
        public DuLieuThuNghiem()
        {
            InitializeComponent();
        }
        // Khai bao cac bien can thiet
        Para CPara = new Para();
        public struct MUNode
        {
            public string id;
            public double x;
            public double y;

        };
        public struct DataItem
        {
            public int id;
            public string item;
            public string datetime;
            public int timetl;
            public int size;
        };

        List<MUNode> listOfNode = new List<MUNode>(); // Danh sach id Node
        List<DataItem> listOfDataItem = new List<DataItem>(); // Danh sach item
        List<List<int>> listMUNode = new List<List<int>>(); // Danh sach MU Node
        List<List<int>> listHLC = new List<List<int>>(); // Danh sach HLC
        List<int> idCh = new List<int>();// Danh sach Item change

        private int _numOfNode;
        public int numOfBSData;
        public int ttl = 5;
        public int numOfItemChange = 0;
        private string _defaultPath = "";
        private string _kichBanPath = "";
        private string _ketQuaPath = "";
        private string _hlcPath = "";
        private string _itemChangePath = "";
        public List<string> lPathThongTinDL = new List<string>();

        #region GetAndSet
        public int NumOfNode
        {
            get
            {
                return this._numOfNode;
            }
            set
            {
                this._numOfNode = value;
            }
        }
        public string DefaultPath
        {
            get
            {
                return this._defaultPath;
            }
            set
            {
                this._defaultPath = value;
            }
        }
        public string KichBanPath
        {
            get
            {
                return this._kichBanPath;
            }
            set
            {
                this._kichBanPath = value;
            }
        }
        public string KetQuaPath
        {
            get
            {
                return this._ketQuaPath;
            }
            set
            {
                this._ketQuaPath = value;
            }
        }
        public string HLCPath
        {
            get
            {
                return this._hlcPath;
            }
            set
            {
                this._hlcPath = value;
            }
        }
        public string ItemChangePath
        {
            get
            {
                return this._itemChangePath;
            }
            set
            {
                this._itemChangePath = value;
            }
        }

        #endregion

        public void getNumOfNode(int num)
        {
            NumOfNode = num;
        }


        public void DuLieuThuNghiem_Load(object sender, EventArgs e)
        {
            cmbnumOfData.Text = "1000";
            _defaultPath = txtPath.Text.ToString();
            // Tao duong dan 

            lPathThongTinDL.Clear();
            lPathThongTinDL.Add(DefaultPath); // Add Default path
            // Tao duong dan 
            KichBanPath = DefaultPath + "\\" + "KichBan";
            KetQuaPath = DefaultPath + "\\" + "KetQua";
            if (Directory.Exists(@KichBanPath) == false)
            {
                Directory.CreateDirectory(@KichBanPath);
            }
            if (Directory.Exists(@KetQuaPath) == false)
            {
                Directory.CreateDirectory(@KetQuaPath);
            }
            lPathThongTinDL.Add(KichBanPath);
            lPathThongTinDL.Add(KetQuaPath);

            // Load cmb Node
            //Duyet qua tung node
            for (int i = 0; i < NumOfNode; i++)
            {
                string id_item = "node_(" + (i + 1).ToString() + ")";
                cmbMUNode.Properties.Items.Add(id_item);
            }
        }


        /// <summary>
        /// Kiem tra Id du lieu co trong list ko
        /// </summary>
        /// <param name="l"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public bool CheckInList(List<int> l, int t)
        {
            bool flag = false;
            for (int i = 0; i < l.Count; i++)
            {
                if (t == l[i])
                    flag = true;
            }
            return flag;
        }


        /// <summary>
        /// Ghi file BS_MU.txt
        /// </summary>
        public void GhiFileBS_MU()
        {
            string id;
            string id_data;
            string date_time;
            string ttl;
            string size;
            string content_BSDATA = "";

            for (int i = 0; i < numOfBSData; i++)
            {
                id = gridBSData.Rows[i].Cells[0].Value.ToString();
                id_data = gridBSData.Rows[i].Cells[1].Value.ToString();
                date_time = gridBSData.Rows[i].Cells[2].Value.ToString();
                ttl = gridBSData.Rows[i].Cells[3].Value.ToString();
                size = gridBSData.Rows[i].Cells[4].Value.ToString();
                content_BSDATA = content_BSDATA + id + "-" + id_data + "-" + date_time + "-" + ttl + "-" + size + "\r\n";
            }
            CPara.WriteFile(@KichBanPath + "\\"+ "BS_data.txt", content_BSDATA);
            MessageBox.Show("Ghi file BS_data thành công! ");
        }

        public void GhiFileHLC()
        {
            // Create HLC
            // Duyet het cac don vi du lieu
            gridHLC.Rows.Clear();
            string HlcOutput = "";
            for (int i = 1; i <= numOfBSData; i++)
            {
                gridHLC.Rows.Add();
                gridHLC.Rows[i - 1].Cells[0].Value = i.ToString();
                gridHLC.Rows[i - 1].Cells[1].Value = listOfDataItem[i - 1].item.ToString();
                string cont = "";
                List<int> lItems = new List<int>();
                // Duyet tung MU trong list MU

                for (int j = 0; j < listMUNode.Count; j++)
                {
                    // Kiem tra co don vi du lieu i trong MU do ko
                    if (CheckInList(listMUNode[j], i - 1) == true)
                    {
                        // Add vao HLC
                        //listHLC[i - 1].Add(j);
                        if (lItems.Count == 0)
                        {
                            cont = cont + (j + 1).ToString();
                        }
                        else
                        {
                            cont = cont + "," + (j + 1).ToString();
                        }
                        lItems.Add(j + 1);
                        listHLC.Add(lItems);
                    }
                }
                gridHLC.Rows[i - 1].Cells[2].Value = cont;

                if (lItems.Count != 0)
                {
                    HlcOutput = HlcOutput + i.ToString() + "-" + cont + "\r\n";
                }
            }
            HLCPath = KichBanPath + "\\" + "BS_HLC.txt";
            lPathThongTinDL.Add(HLCPath);

            CPara.WriteFile(HLCPath, HlcOutput);
            MessageBox.Show("Ghi file HLC_data thành công! ");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ItemsChange"></param>
        /// <param name="IDChangeName"></param>
        public void RandomItemsChange(int ItemsChange, string IDChangeName)
        {
            string IDChangeOut = "";
            Random randomIDChange = new Random();
            //Random ID change

            string currentTime = DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss");
            currentTime = currentTime.Replace("-", "/");

            for (int c = 0; c < ItemsChange; c++)
            {
                int x = 1;
                do
                {
                    x = randomIDChange.Next(1, numOfBSData);
                }
                while (CheckInList(idCh, x) == true);
                // Items change
                string oldItem = gridBSData.Rows[x - 1].Cells[1].Value.ToString();
                string itemChange = oldItem.Remove(1, 1).ToString();
                //MessageBox.Show(x + " " + oldItem + " " + itemChange);
                gridBSData.Rows[x - 1].Cells[1].Value = itemChange;
                gridBSData.Rows[x - 1].Cells[2].Value = currentTime;
                idCh.Add(x);
            }

            for (int i = 0; i < idCh.Count; i++)
            {
                int id = idCh[i];
                DataItem dItem = listOfDataItem[id];

                // Tinh lai size 
                string rcont = id.ToString() + "-" + gridBSData.Rows[id - 1].Cells[1].Value.ToString() + "-" +
                            gridBSData.Rows[id - 1].Cells[3].Value.ToString() + "-" +
                            gridBSData.Rows[id - 1].Cells[2].Value.ToString() + "-";
                int size = rcont.Length + 2;
                //MessageBox.Show(rcont + " " + size.ToString());
                gridBSData.Rows[id - 1].Cells[4].Value = size;

                IDChangeOut = IDChangeOut + id.ToString() + "-" + gridBSData.Rows[id - 1].Cells[2].Value.ToString()
                                + "-" + size.ToString() + "\r\n";
            }
            ItemChangePath= KichBanPath + "\\" + IDChangeName;
            lPathThongTinDL.Add(ItemChangePath);

            MessageBox.Show("Random dữ liệu tới MU thành công....");
            CPara.WriteFile(ItemChangePath, IDChangeOut);

            // Ghi lai file BS_MU voi nhung thay doi 
            GhiFileBS_MU();
        }

        private void btnInit_Click(object sender, EventArgs e)
        {
 
                //Xac dinh so Item can Random de change them
                int num = int.Parse(cmbItemsChange.Value.ToString());
                int ItemsChange = num - numOfItemChange;
                numOfItemChange = num;// Gan lai so luong da change hien tai
                string IDChangeName = "BS_IDchange_" + num.ToString() + ".txt";
                RandomItemsChange(ItemsChange, IDChangeName);
        }

        private void cmbnumOfData_EditValueChanged(object sender, EventArgs e)
        {
            gridBSData.Rows.Clear();
            // Random BS_Data
            numOfBSData = (int)cmbnumOfData.Value;
            Random gen = new Random();
            for (int i = 0; i < numOfBSData; i++)
            {
                int RowSize = 0;
                string id_data;
                id_data = "d0" + (i + 1).ToString();

                //Random date time
                DateTime from = new DateTime(2010, 1, 1);
                DateTime to = new DateTime(2011, 1, 1);
                int range = ((TimeSpan)(to - from)).Days;
                DateTime ran = from.AddDays(gen.Next(range));
                string date_time = ran.ToString("dd/MM/yyyy hh:mm:ss");
                date_time = date_time.Replace("-", "/");
                //MessageBox.Show(date_time);

                //Tinh size tung dong
                string rcont = (i + 1).ToString() + "-" + id_data.ToString() + "-" + date_time.ToString() + "-"
                                + ttl.ToString() + "-";
                RowSize = RowSize + rcont.Length + 2;

                // Dua len Gridview
                gridBSData.Rows.Add();
                gridBSData.Rows[i].Cells[0].Value = i + 1;
                gridBSData.Rows[i].Cells[1].Value = id_data;
                gridBSData.Rows[i].Cells[2].Value = date_time;
                gridBSData.Rows[i].Cells[3].Value = ttl;
                gridBSData.Rows[i].Cells[4].Value = RowSize;

                if (listOfDataItem == null)
                {
                    listOfDataItem.Clear();
                }
                else
                {
                    DataItem dItem = new DataItem();
                    dItem.id = i + 1;
                    dItem.item = id_data;
                    dItem.datetime = date_time;
                    dItem.timetl = ttl;
                    dItem.size = RowSize;
                    listOfDataItem.Add(dItem);
                }
            }         
        }

        private void btnPath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
            folderBrowser.Description = "Select Chase 6 Installation Folder";
            folderBrowser.RootFolder = Environment.SpecialFolder.Desktop;
            folderBrowser.ShowNewFolderButton = true;

            if (folderBrowser.ShowDialog(this) == DialogResult.OK)
            {

                string pt = folderBrowser.SelectedPath;
                txtPath.Text = pt;
                this._defaultPath = pt;

                lPathThongTinDL.Clear();
                lPathThongTinDL.Add(DefaultPath); // Add Default path
                // Tao duong dan 
                KichBanPath = DefaultPath + "\\" + "KichBan";
                KetQuaPath = DefaultPath + "\\" + "KetQua";

                if (Directory.Exists(@KichBanPath) == false)
                {
                    Directory.CreateDirectory(@KichBanPath);
                }
                if (Directory.Exists(@KetQuaPath) == false)
                {
                    Directory.CreateDirectory(@KetQuaPath);
                }

                lPathThongTinDL.Add(KichBanPath); 
                lPathThongTinDL.Add(KetQuaPath);
            } 
        }

        private void btnRandomMU_Click(object sender, EventArgs e)
        {
            GhiFileBS_MU();
            // ---------------- Random MU_Data
            // Random so id du lieu cho MU
            Random r = new Random();
            //minOfDataItem = int.Parse(txtMin.Text.ToString());
            //maxOfDataItem = int.Parse(txtMax.Text.ToString());
            int minOfDataItem = 100;
            int maxOfDataItem = 100;

            listMUNode.Clear();

            // Truong hop 
            if (minOfDataItem == maxOfDataItem)
            {
                for (int i = 0; i < NumOfNode; i++)
                {

                    List<int> id_MUData = new List<int>();

                    for (int j = 0; j < minOfDataItem; j++)
                    {
                        //Random id data 
                        int id_ran;
                        do
                        {
                            id_ran = r.Next(0, numOfBSData);
                        }
                        while (CheckInList(id_MUData, id_ran) == true);
                        id_MUData.Add(id_ran);

                    }

                    // Add items data vao list of Node
                    listMUNode.Add(id_MUData);
                    // Ghi file MU Data
                    string pth = KichBanPath + "\\" + "MU" + (i + 1).ToString() + "_data.txt";
                    string cont = "";
                    for (int k = 0; k < id_MUData.Count; k++)
                    {
                        int id_d = id_MUData[k];
                        cont = cont + gridBSData.Rows[id_d].Cells[0].Value.ToString() + "-"
                            + gridBSData.Rows[id_d].Cells[1].Value.ToString() + "-"
                            + gridBSData.Rows[id_d].Cells[2].Value.ToString() + "-"
                            + gridBSData.Rows[id_d].Cells[3].Value.ToString() + "-"
                            + gridBSData.Rows[id_d].Cells[4].Value.ToString() + "\r\n";
                    }
                    CPara.WriteFile(pth, cont);
                }
                GhiFileHLC();
            }
            else
            {
                for (int i = 0; i < NumOfNode; i++)
                {
                    int num = r.Next(minOfDataItem, maxOfDataItem);// Random so luong data item

                    List<int> id_MUData = new List<int>();

                    for (int j = 0; j < num; j++)
                    {
                        //Random id data 
                        int id_ran = r.Next(0, numOfBSData);

                        if (CheckInList(id_MUData, id_ran) == false)
                        {
                            id_MUData.Add(id_ran); // Add vao list
                        }
                    }

                    // Add items data vao list of Node
                    listMUNode.Add(id_MUData);
                    // Ghi file MU Data
                    string pth = DefaultPath + "\\" + KichBanPath + "\\" + "MU" + (i + 1).ToString() + "_data.txt";
                    string cont = "";
                    for (int k = 0; k < id_MUData.Count; k++)
                    {
                        int id_d = id_MUData[k];
                        cont = cont + gridBSData.Rows[id_d].Cells[0].Value.ToString() + "-"
                            + gridBSData.Rows[id_d].Cells[1].Value.ToString() + "-"
                            + gridBSData.Rows[id_d].Cells[2].Value.ToString() + "-"
                            + gridBSData.Rows[id_d].Cells[3].Value.ToString() + "-"
                            + gridBSData.Rows[id_d].Cells[4].Value.ToString() + "\r\n";
                    }
                    CPara.WriteFile(pth, cont);
                }
                GhiFileHLC();
            }
        }

        private void cmbMUNode_SelectedIndexChanged(object sender, EventArgs e)
        {
            gridMUData.Rows.Clear();// Clear Grid View

            int index = cmbMUNode.SelectedIndex;// Lay ra MU tuong ung duoc chon

            //MessageBox.Show(index.ToString());
            List<int> lID = listMUNode[index];

            for (int i = 0; i < lID.Count; i++)
            {
                int idItem = lID[i];// Lay tung don vi du lieu tuong ung voi MU
                DataItem dItem = listOfDataItem[idItem];
                gridMUData.Rows.Add();
                gridMUData.Rows[i].Cells[0].Value = dItem.id.ToString();
                gridMUData.Rows[i].Cells[1].Value = dItem.item.ToString();
                gridMUData.Rows[i].Cells[2].Value = dItem.datetime.ToString();
                gridMUData.Rows[i].Cells[3].Value = dItem.timetl.ToString();
                gridMUData.Rows[i].Cells[4].Value = dItem.size.ToString();

            }
        }
    }
}
